﻿using System;

namespace diantou.web.onlineJudge
{
    /// <summary>
    /// 问题
    /// </summary>
    /// <typeparam name="viewType">页面类型</typeparam>
    abstract class problemView<viewType> : view<viewType>
        where viewType : problemView<viewType> 
    {
        /// <summary>
        /// 问题
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.problem problem;
        /// <summary>
        /// 用户OJ问题
        /// </summary>
        protected userProblem userProblem
        {
            get { return new userProblem { Problem = problem, User = currentUser }; }
        }
        /// <summary>
        /// 相关收藏top5
        /// </summary>
        protected userProblem.relateFavorite relateFavorite
        {
            get
            {
                return new userProblem.relateFavorite { ViewOnly = requestHeader.IsReView, Problem = new userProblem { Problem = problem, User = currentUser } };
            }
        }
        /// <summary>
        /// 用户图片集合
        /// </summary>
        protected focusImage acceptedImage
        {
            get
            {
                if (requestHeader.IsReView) return new focusImage { ViewOnly = true };
                return new focusImage(diantou.dataAccess.clientCache.User.Get(diantou.dataAccess.tcpCall.judge.getAceptedNewUserIds(problem.Id, 4 * 10)), 10);
            }
        }
        /// <summary>
        /// AC排行前3名
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.judge.problemPublicAcceptedTop3? publicAcceptedRankTop3;
        /// <summary>
        /// AC排行前3名
        /// </summary>
        protected diantou.dataAccess.judge.problemPublicAcceptedTop3 acceptedRankTop3
        {
            get
            {
                if (publicAcceptedRankTop3 == null)
                {
                    publicAcceptedRankTop3 = diantou.dataAccess.tcpCall.judge.getPublicAcceptedTop3(problem.Id);
                }
                return (diantou.dataAccess.judge.problemPublicAcceptedTop3)publicAcceptedRankTop3;
            }
        }
        /// <summary>
        /// 是否验证问题权限
        /// </summary>
        protected virtual bool isShowPermission
        {
            get { return false; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="problemId"></param>
        /// <returns></returns>
        protected virtual bool loadView(int problemId)
        {
            if (problemId == 0)
            {
                checkSearchEngine(diantou.dataModel.path.pub.Default.OnlineJudgeAllProblemPath, webConfig.NotFound404);
                return false;
            }
            problem = diantou.dataAccess.clientCache.Problem[problemId];
            if (problem != null)
            {
                if (problem.ShowType != dataModel.problem.showType.None) return true;
                if (isShowPermission && currentUser != null && currentUser.IsOnlineJudgeEditor(problem)) return true;
                problem = null;
            }
            AjaxResponse(webConfig.NotFound404);
            return false;
        }
    }
}
